/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：VariableTableViewPage.js<br>
 * 日期：2017年9月4日<br>
 * 作者: 李伟利<br>
 * 功能：实验变量数据表格的创建<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 * 
 ******************************************************************************/
Rainier.UI.V2.VariableTableViewPage = Rainier.Basic.CompositeObject.extend({
    
    //类名称
    NAME : "com.rainier.application.control.VariableTableViewPage",
    
    
    /**
     * [init 初始化]
     * @param  {[type]} parent     [description]
     * @param  {[type]} properties [description]
     * @return {[type]}            [description]
     */
    init:function(parentComposite){
        this._super();
        this.variableDataGrid      = null;   //实验变量表格
        this.tableContainerDialog  = null;   //表格容器对话框
        this.dataGridValueDisplay  = null;    //表格容器对话框列的创建
        this.parentContainer       = $("#application");//作为dialog的父组件容器
        this.expermentalResult     = app.getResultComponent();
        this.variableTable         = parentComposite || this.expermentalResult.variablePanel;
    },
    
    /**
     * @名称   VariableTableViewPage
     * @功能   实验变量数据表格的创建
     * @author 李伟利
     * @data   2017-9-14
     * @param  {[type]}   tab [description]
     * @return {[type]}       [description]
    **/
    createTableViewPage : function(data,falg){
//     var expctx = app.resultViewPage;
//     var variableTable = expctx.variablePanel;
       //实验变量面板清空
       this.variableTable.clear();
       var matData = data.asArray();
       $.each(matData,function(index,data){
          var valueListData = data.valueList;
       	  if(falg){
       	      valueListArray = new draw2d.util.ArrayList();
       	      valueListData.clazz = draw2d.util.ArrayList;
       	      valueListArray.deserialize(valueListData);
       	      valueListData = valueListArray;
       	  }
       	  var dataMax = Math.max.apply(Math,valueListData.asArray());
       	  var dataMin = Math.min.apply(Math,valueListData.asArray());
       	  var valueListData =  valueListData.data;
       	  data.min = dataMin;
       	  data.max  = dataMax;
       	  data.valueList = valueListData;
       })
       this.initInterfaceData(matData);
       this.experimentalVariableForm(matData,this.variableTable);
    },
    
    /**
     * @名称   VariableTableViewPage
     * @功能    实验变量数据表格的创建
     * @author 李伟利
     * @data   2017-9-14
     * @param  {[type]}   tab [description]
     * @return {[type]}       [description]
    **/
    experimentalVariableForm : function(data,parentcom){
    	  var _this = this;
	      var Gridproperties = {
	      	 fit:true,
	         fitColumns:true,
	         singleSelect:true,
	         showFooter : true,
	         scrollbarSize : 18,
	         resizable:true,
	         columns : [[
	               {field:'name',title:'Name',width:"20%",align:"center"},
	               {field:'type',title:'Type',width:"20%",align:"center"},
	               {field:'valueList',title:'Value',width:"20%",align:"center"},    
	               {field:'min',title:'Min',width:"20%",align:"center"},
	               {field:'max',title:'Max',width:"20%",align:"center"}
	            ]],
	      }
	      var dataRowGrid = {"rows":data};
	      this.variableDataGrid = new Rainier.UI.V2.UICompositeEditorDataGrid(parentcom,Gridproperties);
	      this.variableDataGrid.getElement().datagrid({data:dataRowGrid});//数据加载，解决表格列与行不对齐问题
	      
	      //初始化表格编辑
	      _this.initDataGridEditor();
	      

       var Listener = Rainier.UI.V2.UIEventListener.extend({
          
          NAME : "Listener",

          init : function(){
            this._super();
          },

          onEvent : function(sEvent,eventSource,args){

            var index = args[0];
            var row   = args[1];
            _this.valueDataDisplay(index,row);
            
          }

       })

       this.variableDataGrid.registerEventListener("onDblClickRow",new Listener());
       // this.variableDataGrid.addEventListener("onDblClickRow",function(index,row){
       //       _this.valueDataDisplay(index,row);
       // });  
   },
    /**
     * @名称   VariableTableViewPage
     * @功能   数据变量value值的呈现
     * @author 李伟利
     * @data   2017-9-14
     * @param  {[type]}   tab [description]
     * @return {[type]}       [description]
    **/
   
   valueDataDisplay : function(index,row){ 
   	   if(row.name !=''){
       	    var dialogProperties = {
                title : 'All value display',
                width : 500,
                height: 500,
                closed: false,
                modal : true,
                iconCls:'icon-edit',
            };
           
            this.tableContainerDialog = new Rainier.UI.V2.UICompositeDialog(this.parentContainer,dialogProperties);
            this.loadDataColumns(row,this.tableContainerDialog);
            this.loadDataRow(row);   
       }
   },
   /**
     * @名称   VariableTableViewPage
     * @功能   加载value数据表格columns
     * @author 李伟利
     * @data   2017-9-14
     * @param  {[type]}   tab [description]
     * @return {[type]}       [description]
    **/
   loadDataColumns : function(row,parentTableContainer){
        var $datagridColumns = {};
        var dataColumns = [];
        var columns = row.columns;
        var gridWidth = 0;
        parseInt(columns) < 5 || columns==undefined ?columns=5:columns;
        for(var i=0;i<parseInt(columns)+1;i++){
             i==0 ? gridWidth=40 : gridWidth=100;
             dataColumns.push({field:i+"ii",width:gridWidth,align:"center",title:i}); 
        }
        $datagridColumns = new Array(dataColumns);
        
        var dialogGridProperties = {
            fit:true,
            fitColumns:true,
            singleSelect:true,
            showFooter : true,
            scrollbarSize : 18,
            resizable:true,
            columns : $datagridColumns
        }
        this.dataGridValueDisplay = new Rainier.UI.V2.UICompositeEditorDataGrid(parentTableContainer,dialogGridProperties);
   },
   /**
     * @名称   VariableTableViewPage
     * @功能   加载value数据表格row
     * @author 李伟利
     * @data   2017-9-14
     * @param  {[type]}   tab [description]
     * @return {[type]}       [description]
    **/
   loadDataRow : function(row){
        var rowDataArray = [];
        var rowValueList = row.valueList;
        var rowValueLen  = rowValueList.length;
        var objCol = row.columns;
        var objRow = row.rows;
        var num;
        if(objCol == 1 || objCol == undefined){
            $.each(rowValueList,function(i,dataRow){
                var dataObj = {};
                dataObj[0+"ii"] = i+1;
                dataObj[1+"ii"] = rowValueList[i];
                rowDataArray.push(dataObj);
            })
        }else if(objRow == "1" || objRow==undefined){
            var dataObj = {};
            $.each(rowValueList, function(i,dataRow) {
                dataObj[0+"ii"] = 1;
                dataObj[i+1+"ii"] = rowValueList[i];
            });
            rowDataArray.push(dataObj);
        }else{
            for(var i=0;i<objRow;i++){  
                var dataObj = {};
                for(var j=0;j<objCol;j++){
                    objCol == 2 ? num = 3 : num=4;
                    i==0 ? k=j:k=i*num+j-i;             
                    dataObj[0+"ii"] = i+1;
                    dataObj[j+1+"ii"] = rowValueList[k];
                }
                rowDataArray.push(dataObj);
             }
        }
        this.initInterfaceData(rowDataArray);
        this.dataGridValueDisplay.loadData(rowDataArray);
   },
   /**
     * @名称   VariableTableViewPage
     * @功能   填充界面空数据，使界面更美观
     * @author 李伟利
     * @data   2017-9-14
     * @param  {[type]}   tab [description]
     * @return {[type]}       [description]
    **/
   initInterfaceData : function(data){
       if(data.length < 10){
            for(var i=0;i<35;i++){
                var dataGrid = {min:"",max:"",name:"",valueList:""};
                data.push(dataGrid);
            }
        }
   },
   /**
     * @名称   VariableTableViewPage
     * @功能   初始化表格编辑
     * @author 李伟利
     * @data   2017-9-14
     * @param  {[type]}   tab [description]
     * @return {[type]}       [description]
    **/
   initDataGridEditor : function(){
   	 var _this = this;
	   	$.extend($.fn.datagrid.methods, {
			editCell: function(jq,param){
				return jq.each(function(){
					var opts   = $(this).datagrid('options');
					var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
					for(var i=0; i<fields.length; i++){
						var col = $(this).datagrid('getColumnOption', fields[i]);
						col.editor1 = col.editor;
						if (fields[i] != param.field){
							col.editor = null;
						}
					}
					$(this).datagrid('beginEdit', param.index);
					for(var i=0; i<fields.length; i++){
						var col = $(this).datagrid('getColumnOption', fields[i]);
						col.editor = col.editor1;
					}
				});
			}
	    });    
   },
   
   
   /**
     * @名称   VariableTableViewPage
     * @功能   结束表格编辑
     * @author 李伟利
     * @data   2017-9-14
     * @param  {[type]}   tab [description]
     * @return {[type]}       [description]
    **/
   endEditing : function(){
   	var _this  = this;
   	    if (_this.editIndex == undefined){return true}
			if ($('#boxValue_dg').datagrid('validateRow', _this.editIndex)){
				$('#boxValue_dg').datagrid('endEdit', _this.editIndex);
				_this.editIndex = undefined;
				return true;
			} else {
				return false;
		}
   },  
});
         